package linz
import kotlin.reflect.KProperty


@Volatile
@Suppress
var p:String = ""

fun main() {

    printAllValues<RGB>() // prints RED, GREEN, BLUE

//    println(::p.name)
    //传入无参 类似 println(new Foo())
//    println(::Foo)
//    function(::Foo)
    var name = null
    val s = name ?:  "var is null"
    println(s)
    foo()

    s.length.also {
        println("\nHello world = $s")
    }

    println(s.length)

    /**
     * 1. field delegate
     */
    val e = Example()
//    println(e.p)
    e.p = "New Value"

    val myClass = MyClass()
    // Notification: 'oldName: Int' is deprecated.
    // Use 'newName' instead
    myClass.oldName = 42
    println(myClass.newName) // 42



    val user = User(mapOf(
        "name" to "John Doe",
        "age"  to 25,
        "sex" to 1
    ))
    println(user.name) // Prints "John Doe"
    println(user.age)  // Prints 25
}

class Foo
fun function(factory: () -> Foo) {
    val x: Foo = factory()
    println(x)
}

// return and jump
fun foo() {
    run lit@  {listOf(1, 2, 3, 4, 5).forEach {
        if (it == 3) return@lit // local return to the caller of the lambda - the forEach loop
        print(it)
    }}
    print(" done with explicit label")
}

class Delegate {
    operator fun getValue(thisRef: Any?, property: KProperty<*>): String {
        return "$thisRef, thank you for delegating '${property.name}' to me!"
    }

    operator fun setValue(thisRef: Any?, property: KProperty<*>, value: String) {
        println("$value has been assigned to '${property.name}' in $thisRef.")
    }
}
class Example {
    var p: String by Delegate()
}


class MyClass {
    var newName: Int = 0
    @Deprecated("Use 'newName' instead", ReplaceWith("newName"))
    var oldName: Int by this::newName
}



class User(val map: Map<String, Any?>) {
    val name: String by map
    val age: Int     by map
}





